#include <boost/algorithm/string.hpp>
#include "Address.h"

using namespace kc_sf;

// ---------------------------------------------------- //

Address::Address(
		const std::string &localpart,
		const std::string &domain)
: aLocalpart(localpart), aDomain(domain)
{
}

// ---------------------------------------------------- //

bool Address::equals(const Address &a) const
{
	namespace ba = boost::algorithm;

	bool match = ba::iequals(a.aLocalpart, aLocalpart);

	if (match)
	{
		match = ba::iequals(a.aDomain, aDomain);
	}

	return match;
}

// ---------------------------------------------------- //

std::string Address::toString () const
{
	return aDomain.empty() ? "-"
			: aLocalpart.empty()  ? "@" + aDomain
			: aLocalpart + "@" + aDomain;
}


namespace kc_sf
{
	// ---------------------------------------------------- //

	bool operator == ( const Address &a1, const Address &a2 )
	{
		return a1.aLocalpart == a2.aLocalpart
				&& a1.aDomain == a2.aDomain;
	}

	// ---------------------------------------------------- //

	bool operator != ( const Address &a1, const Address &a2 )
	{
		return a1.aLocalpart != a2.aLocalpart
				|| a1.aDomain != a2.aDomain;
	}

	// ---------------------------------------------------- //

	std::ostream & operator << (std::ostream &out, const Address &a)
	{
		bool display_name = !a.aDisplayName.empty();
		if (display_name)
			out << a.aDisplayName << " <";

		out << a.aLocalpart << "@" << a.aDomain;

		if (display_name)
			out << ">";

		return out;
	}
}
